package in.muton.edushare.academics.panels;

import in.muton.edushare.academics.panels.GradePanelUtil.GradeKeyHelper;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.dao.academics.grades.UnivLetterGrades;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.Universities;

public class UnivGradeKeyEdit extends EdusharePanel {
	private IModel<Universities> univModel = null;
	private UnivGradeKeyPanel panel;
	private List<GradeKeyHelper> GradeKeyHelperList = new ArrayList<GradeKeyHelper>();
	private boolean initialized = false;

	public UnivGradeKeyEdit(String id, IModel<Universities> model,
			EduShareBasePage page, UnivGradeKeyPanel panel) {
		super(id, model, page);
		this.univModel = model;
		this.panel = panel;

		Form<Void> f = new Form<Void>("form");
		add(f);
		f.add(new Button("save") {
			private static final long serialVersionUID = -1043729086512850530L;

			@Override
			public void onSubmit() {
				if (saveGrades()) {
					initialized = false;
					setError("The grades have been saved.");
					UnivGradeKeyEdit.this.panel
							.setActivePanel(UnivGradeKeyPanel.UNIV_KEY_VIEW);
				}
			}
		});

		f.add(new Button("discard") {
			private static final long serialVersionUID = -1043729086512850530L;

			@Override
			public void onSubmit() {
				UnivLetterGrades ugl = new UnivLetterGrades(getORMInterface());
				ugl.discardCurrentSet(univModel.getObject());
				initialized = false;
			}
		});

		f.add(new SubmitLink("addGradeKeyHelper") {
			@Override
			public void onSubmit() {
				GradeKeyHelper t = new GradeKeyHelper();
				GradeKeyHelperList.add(t);
				Collections.sort(GradeKeyHelperList);
				Collections.reverse(GradeKeyHelperList);
			}

			private static final long serialVersionUID = -9196223340220305645L;
		});

		f.add(new ListView<GradeKeyHelper>("gradeList",
				new IModel<List<GradeKeyHelper>>() {
					public List<GradeKeyHelper> getObject() {
						initGradeKeyHelpers();
						Collections.sort(GradeKeyHelperList);
						return GradeKeyHelperList;
					}

					public void setObject(List<GradeKeyHelper> arg0) {
					}

					public void detach() {
					}

					private static final long serialVersionUID = 5533847989282188233L;
				}) {
			@Override
			protected void populateItem(ListItem<GradeKeyHelper> arg0) {
				final GradeKeyHelper t = arg0.getModelObject();
				arg0.add(new TextField<String>("gradeLetter",
						new PropertyModel<String>(t, "gradeName")));

				arg0
						.add(new TextField<Double>("minMarks",
								new PropertyModel<Double>(t, "minMarks"),
								Double.class));

				arg0
						.add(new TextField<Double>("maxMarks",
								new PropertyModel<Double>(t, "maxMarks"),
								Double.class));

				arg0
						.add(new TextField<Double>("gpaValue",
								new PropertyModel<Double>(t, "gpaValue"),
								Double.class));

				arg0.add(new CheckBox("fail", new Model<Boolean>() {
					@Override
					public Boolean getObject() {
						return !t.passGrade;
					}

					@Override
					public void setObject(Boolean b) {
						t.passGrade = !b;
					}

					private static final long serialVersionUID = 8912571482905107014L;
				}));

				arg0.add(new SubmitLink("addMore") {
					private static final long serialVersionUID = 4452066489597929288L;

					@Override
					public void onSubmit() {
						GradeKeyHelper t = new GradeKeyHelper();
						GradeKeyHelperList.add(t);
						Collections.sort(GradeKeyHelperList);
						Collections.reverse(GradeKeyHelperList);
					}
				});

				arg0.add(new Link<Void>("delete") {
					private static final long serialVersionUID = 4452066489597929288L;

					@Override
					public void onClick() {
						GradeKeyHelperList.remove(t);
						GradePanelUtil.removeTuple(t, univModel.getObject());
					}
				});
			}

			private static final long serialVersionUID = -5279391076526942442L;
		});

	}

	@Override
	public boolean isVisible() {
		return panel.getActivePanel() == UnivGradeKeyPanel.UNIV_KEY_EDIT;
	}

	private boolean saveGrades() {
		UnivLetterGrades ugl = new UnivLetterGrades(getORMInterface());
		Collections.sort(GradeKeyHelperList);
		Collections.reverse(GradeKeyHelperList);
		for (int i = 0; i < GradeKeyHelperList.size() - 1; i++) {
			if (!StringUtil.isValid(GradeKeyHelperList.get(i).gradeName)) {
				setError("Please provide valid names for the grades");
				return false;
			}
			if (GradeKeyHelperList.get(i).gpaValue < 0) {
				setError("Please provide valid number for GPA value");
				return false;
			}
			if (GradeKeyHelperList.get(i).maxMarks == GradeKeyHelperList
					.get(i + 1).minMarks) {
				setError("The maximum marks for one grade should be equal to minimum marks for next grade");
				return false;
			}
		}
		ugl.discardCurrentSet(univModel.getObject());
		for (GradeKeyHelper t : GradeKeyHelperList) {
			ugl.addLetterGrade(univModel.getObject(), t.gradeName, t.gpaValue,
					t.maxMarks, t.minMarks, t.passGrade);
		}
		return true;
	}

	private void initGradeKeyHelpers() {
		if (!initialized) {
			GradeKeyHelperList = GradePanelUtil
					.getTuples(univModel.getObject());
			initialized = true;
		}
	}

	private static final long serialVersionUID = 7598485418242680629L;
}
